package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@TableBind(tableName="account_role",pkName = "id")
public class AccountRole extends BasicModel<AccountRole> {
    public static final AccountRole dao = new AccountRole();
    public static final String SELECT_ALL = "`id`,`name`,`code`,`create_time`,`remark`,`status`,`platform`";

    @Override
    public Page<AccountRole> paginate(Integer pageNumber, Integer pageSize, String column, AccountRole model) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName() + " WHERE platform & "+ CCRM +" = "+ CCRM);
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    @Override
    public boolean save() {
        setPlatform(CCRM);
        return super.save();
    }

    public AccountRole setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public AccountRole setName(String name){
        return this.set("name",name);
    }

    public String getName(){
        return this.getStr("name");
    }
        
    public AccountRole setCode(String code){
        return this.set("code",code);
    }

    public String getCode(){
        return this.getStr("code");
    }
        
    public AccountRole setCreateTime(Date createTime){
        return this.set("create_time",createTime);
    }

    public Date getCreateTime(){
        return this.getDate("create_time");
    }
        
    public AccountRole setRemark(String remark){
        return this.set("remark",remark);
    }

    public String getRemark(){
        return this.getStr("remark");
    }
        
    public AccountRole setStatus(Integer status){
        return this.set("status",status);
    }

    public Integer getStatus(){
        return this.get("status");
    }

    public AccountRole setPlatform(Integer platform){
        return this.set("platform",platform);
    }

    public Integer getPlatform(){
        return this.get("platform");
    }
}